﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GWA.GriWebServ;
using System.Web.Profile;
using System.Web.Security;
using GWA.Controls;

namespace GWA.Controls
{
    public partial class PatientEdit : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string id = Request.QueryString["id"].ToString();
            if (!Page.IsPostBack)
                GetPatientRecord(id);
        }

        private void GetPatientRecord(string PatientID)
        {
            Service1 s1 = new Service1();
            string[] doctors = Roles.GetUsersInRole("Doctor");
            ddlDoctors.DataSource = doctors;
            ddlDoctors.DataBind();

            Patient pat = s1.GetPatientByID(PatientID);

            txtAddr.Text = pat.Address;
            txtCity.Text = pat.City;
            txtFName.Text = pat.FirstName;
            txtLName.Text = pat.LastName;
            txtPhone.Text = pat.Phone;
            txtZip.Text = pat.Zip;
            ddlState.SelectedValue = pat.State;
            rdbHealthcare.SelectedValue = pat.Healthcare.ToString();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            Patient pat = new Patient();
            MembershipUser user = Membership.GetUser(Page.User.Identity.Name);
            Service1 s1 = new Service1();

            pat.FirstName = txtFName.Text;
            pat.LastName = txtLName.Text;
            pat.Address = txtAddr.Text;
            pat.City = txtCity.Text;
            pat.Zip = txtZip.Text;
            pat.State = ddlState.SelectedValue;
            pat.Phone = txtPhone.Text;
            pat.Healthcare = Convert.ToBoolean(rdbHealthcare.SelectedValue.ToString());
            pat.UserID = user.ProviderUserKey.ToString();
            s1.UpdatePatient(pat);
            Response.Redirect(LoginPanel.AccessPage(Page.User.Identity.Name));
        }

        protected void btnScheduleAppt_Click(object sender, EventArgs e)
        {
            Appointment appt = new Appointment();
            MembershipUser pat = Membership.GetUser(Page.User.Identity.Name);
            MembershipUser doc = Membership.GetUser(ddlDoctors.SelectedValue.ToString());
            Service1 s1 = new Service1();

            appt.ApptReason = txtReason.Text;
            appt.ApptTime = Convert.ToDateTime(txtDate.Text + " " + ddlTime.SelectedValue);
            appt.PatientID = pat.ProviderUserKey.ToString();
            appt.DrID = doc.ProviderUserKey.ToString();
            s1.SaveAppt(appt);
            Response.Redirect(LoginPanel.AccessPage(Page.User.Identity.Name));
        }
    }
}